$(document).ready(function(){						   
	// Handles for non-Html5-supported Browser
	showInitView();
	$.base64.utf8encode = true;
	var tempPassword='';
	var onblur=false;
  $("#username").bind('blur',function(){
		if($("#username").val()==''){
			 $("#errorUsername").removeClass("invisible").addClass("error");
			 $("#errorUsername").html("Please input Username.");
		 }else{
			 $("#errorUsername").removeClass("error").addClass("invisible");
			 $("#errorUsername").html("");
		 }
	 });
	
	$("#password").bind('blur',function(){
		if($("#password").val()==''){
		  $("#errorPassword").removeClass("invisible").addClass("error");
			 $("#errorPassword").html("Please input Password.");
		 }else{
			 $("#errorPassword").removeClass("error").addClass("invisible");
			 $("#errorPassword").html("");
			 if(onblur){
				 $("#password").val(tempPassword);
				 alert($("#password").val());
				 onblur=false;
			 }
		}
	});
	
	$("#password").bind('keypress',function(){
		//if(tempPassword!=''){
		tempPassword=$.base64.btoa($.md5($("#password").val()));
		onblur=true;
		//}
	});
	
//	$("#btLogin").click(function(){
//		//alert("Success!");
//		$("#fmLogin").submit();
//	});
	
	$("#btLogin").bind('keypress click',function(){	
		checkInputValue();
		if(checkInputValue()){
			//alert("Success!");
			$("#fmLogin").submit();
			//$("#fmLogin")[0].checkValidity();
		 }
//		else{
			//alert("Failed");
//		}
	});
		
		
	//});	
	/**
	 * @name showInitView
	 * @comment show initial views 
	 */
	function showInitView(){
		if(!checkhHtml5()){
			// Add placeholder
			placeHolder($("#username")[0],true);
            placeHolder($("#password")[0],true);
		}else{
			// other Special Handles
		}
	}
 
  	/**
 	 * @name checkInputValue
 	 * @comment input check
 	 * @return {Boolean} 
 	 */
	function checkInputValue(){
		// Add check rules
		  var username=$("#username").val();
		  var password=$("#password").val();
		  if(username==''||password==''){
			  if(username==''){
				 $("#errorUsername").removeClass("invisible").addClass("error");
				 $("#errorUsername").html("Please input Username.");
			  }else{
				 $("#errorUsername").removeClass("error").addClass("invisible");
			     $("#errorUsername").html("");
			  }
			  if(password==''){
				 $("#errorPassword").removeClass("invisible").addClass("error");
				 $("#errorPassword").html("Please input Password.");
			  }else{
				 $("#errorPassword").removeClass("error").addClass("invisible");
			     $("#errorPassword").html("");
			     return false;
			  }
		  }else{
			  $("#errorUsername").removeClass("error").addClass("invisible");
			  $("#errorUsername").html("");
			  $("#errorPassword").removeClass("error").addClass("invisible");
			  $("#errorPassword").html("");
			  return true;
		  }
	}
	
 	/**
 	 * @name checkhHtml5
 	 * @class html5 check
 	 * @return {Boolean} 
 	 */
	function checkhHtml5()   
	{   
		if (typeof(Worker) !== "undefined")   
		{   
			return true;   
		} else   
		{   
			return false; 
		}  
	} 
	
	
 	/**
 	 * @name placeHolder
 	 * @class 跨浏览器placeHolder,对于不支持原生placeHolder的浏览器，通过value或插入span元素两种方案模拟
 	 * @param {Object} obj 要应用placeHolder的表单元素对象
 	 * @param {Boolean} span 是否采用悬浮的span元素方式来模拟placeHolder，默认值false,默认使用value方式模拟
  	 */
	function placeHolder(obj, span) {
		if (!obj.getAttribute('placeholder')) return;
		var imitateMode = span === true ? true: false;
		var supportPlaceholder = 'placeholder' in document.createElement('input');
		if (!supportPlaceholder) {
			var defaultValue = obj.getAttribute('placeholder');
			if (!imitateMode) {
				obj.onfocus = function() { (obj.value == defaultValue) && (obj.value = '');
					obj.style.color = '';
				}
				obj.onblur = function() {
					if (obj.value == defaultValue) {
						obj.style.color = '';
					} else if (obj.value == '') {
						obj.value = defaultValue;
						obj.style.color = '#ACA899';
					}
				}
				obj.onblur();
			} else {
				var placeHolderCont = document.createTextNode(defaultValue);
				var oWrapper = document.createElement('span');
				oWrapper.style.cssText = 'position:absolute; color:#ACA899; display:inline-block; overflow:hidden;';
				oWrapper.className = 'wrap-placeholder';
				oWrapper.style.fontFamily = getStyle(obj, 'fontFamily');
				oWrapper.style.fontSize = getStyle(obj, 'fontSize');
				oWrapper.style.marginLeft = parseInt(getStyle(obj, 'marginLeft')) ? parseInt(getStyle(obj, 'marginLeft')) + 3 + 'px': 3 + 'px';
				oWrapper.style.marginTop = parseInt(getStyle(obj, 'marginTop')) ? getStyle(obj, 'marginTop') : 1 + 'px';
				oWrapper.style.paddingLeft = getStyle(obj, 'paddingLeft');
				oWrapper.style.width = obj.offsetWidth - parseInt(getStyle(obj, 'marginLeft')) + 'px';
				oWrapper.style.height = obj.offsetHeight + 'px';
				oWrapper.style.lineHeight = obj.nodeName.toLowerCase() == 'textarea' ? '': obj.offsetHeight + 'px';
				oWrapper.appendChild(placeHolderCont);
				obj.parentNode.insertBefore(oWrapper, obj);
				oWrapper.onclick = function() {
					obj.focus();
				}
				//绑定input或onpropertychange事件
				if (typeof(obj.oninput) == 'object') {
					obj.addEventListener("input", changeHandler, false);
				} else {
					obj.onpropertychange = changeHandler;
				}
				function changeHandler() {
					oWrapper.style.display = obj.value != '' ? 'none': 'inline-block';
				}
				
 				/**
 				 * @name getStyle
 				 * @class 获取样式
 				 * @param {Object} obj 要获取样式的对象
 				 * @param {String} styleName 要获取的样式名
 				 */
				function getStyle(obj, styleName) {
					var oStyle = null;
					if (obj.currentStyle) oStyle = obj.currentStyle[styleName];
					else if (window.getComputedStyle) oStyle = window.getComputedStyle(obj, null)[styleName];
					return oStyle;
				}
			}
		}
	}	
});